import 'package:deepseek_desktop/components/Chat.dart';
import 'package:deepseek_desktop/components/ChatHistoryController.dart';
import 'package:deepseek_desktop/components/ChatHistoryList.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:deepseek_desktop/controllers/TextFieldController.dart';

class ChatsView extends StatelessWidget {
  final Color textColor = const Color.fromARGB(255, 115, 98, 140);

  final Color bgDarkColor = const Color.fromARGB(255, 54, 37, 79);

  final Color bgColor = const Color.fromARGB(150, 64, 46, 88);

  final ChatHistoryController chatHistoryController =
      Get.put(ChatHistoryController());
  final TextFieldController textFieldController =
      Get.put(TextFieldController());

  ChatsView({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Container(
          width: 240,
          color: bgColor,
          child: Column(
            children: [
              Container(
                decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(
                  width: 1,
                  color: bgDarkColor,
                ))),
                child: Container(
                    padding: const EdgeInsets.all(10),
                    child: SafeArea(
                      child: Row(
                        children: [
                          Expanded(
                              child: Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(15),
                            ),
                            child: Obx(() => TextField(
                                  cursorColor: Colors.black.withAlpha(255),
                                  cursorWidth: 1,
                                  controller:
                                      textFieldController.textController,
                                  maxLines: 1,
                                  minLines: 1,
                                  decoration: InputDecoration(
                                    border: InputBorder.none,
                                    isDense: true,
                                    contentPadding: const EdgeInsets.symmetric(
                                        vertical: 10, horizontal: 0),
                                    hintText: '搜索',
                                    hintStyle: TextStyle(color: textColor),
                                    prefixIcon: const Icon(Icons.search,
                                        color: Colors.indigoAccent),
                                    prefixIconConstraints:
                                        const BoxConstraints.tightFor(
                                            width: 30, height: 20),
                                    suffixIcon: textFieldController
                                            .text.value.isNotEmpty
                                        ? IconButton(
                                            padding: EdgeInsets.zero,
                                            constraints: const BoxConstraints(
                                              minWidth: 30,
                                              minHeight: 30,
                                            ),
                                            icon: Container(
                                              decoration: const BoxDecoration(
                                                color: Colors.grey,
                                                shape: BoxShape.circle,
                                              ),
                                              padding: const EdgeInsets.all(3),
                                              child: const Icon(
                                                Icons.close,
                                                color: Colors.white,
                                                size: 10,
                                              ),
                                            ),
                                            onPressed: () {
                                              textFieldController.clear();

                                              var value = textFieldController
                                                  .text.value;
                                              _handleInputChange(value);
                                            },
                                          )
                                        : null,
                                    suffixIconConstraints: const BoxConstraints(
                                        minWidth: 30, minHeight: 30),
                                  ),
                                  style: TextStyle(
                                      fontSize: 12,
                                      fontFamily:
                                          'Roboto;Arial;Helvetica;Georgia;微软雅黑',
                                      color: textColor),
                                  onChanged: (value) {
                                    textFieldController.text.value = value;
                                    _handleInputChange(value);
                                  },
                                )),
                          )),
                          const SizedBox(
                            width: 10,
                          ),
                          // 原型按钮
                          IconButton(
                            onPressed: () {
                              chatHistoryController.addChat();
                            },
                            style: ButtonStyle(
                              backgroundColor:
                                  WidgetStateProperty.all(Colors.white),
                              overlayColor: WidgetStateProperty.all(
                                  const Color.fromARGB(50, 255, 255, 255)),
                            ),
                            icon: const Icon(Icons.add),
                            color: Colors.indigoAccent,
                            hoverColor: const Color.fromARGB(255, 53, 23, 85),
                            padding: const EdgeInsets.all(0),
                            iconSize: 20,
                            constraints: const BoxConstraints.tightFor(
                                width: 30, height: 30),
                          ),
                        ],
                      ),
                    )),
              ),
              Expanded(child: ChatHistoryList()),
            ],
          ),
        ),
        Expanded(
            child: Container(
          color: Colors.transparent,
          child: Obx(() => chatHistoryController.selectedChatId.value.isEmpty
              ? Container(
                  color: Colors.white,
                  child: const Center(
                      child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(Icons.chat, size: 100, color: Colors.grey),
                      SizedBox(height: 20),
                      Text(
                        "欢迎使用 DeepSeek Desktop",
                        style: TextStyle(
                          fontSize: 20,
                          color: Colors.grey,
                        ),
                      )
                    ],
                  )))
              : Chat(id: chatHistoryController.selectedChatId.value)),
        ))
      ],
    );
  }

  _handleInputChange(String value) {
    if (value.isEmpty) {
      chatHistoryController.items.value =
          chatHistoryController.localhistoryItems;
    } else {
      // 过滤聊天记录
      var list = chatHistoryController.localhistoryItems
          .where(
              (item) => item.name.toLowerCase().contains(value.toLowerCase()))
          .toList();
      chatHistoryController.items.value = list;
    }
  }
}
